<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.courses_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

     <el-form-item label="报名时间">
        <el-date-picker size="small" v-model="uploadTime" @change="handleQuery" unlink-panels :picker-options="pickerOptions" 　 style="width: 220px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']"></el-date-picker>
    </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:userCourses:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:userCourses:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:userCourses:remove']"
        >删除</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['course:userCourses:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userCoursesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="订单编号" align="center" prop="orderNo" min-width="150" />
      <el-table-column label="用户名称" align="center" prop="userName" min-width="150" />
      <el-table-column label="课程名称" align="center" prop="courseName" min-width="220" />
      <el-table-column label="用户成员" align="center" prop="userMemberName" min-width="120"/>
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="报名节数" align="center" prop="bookNum" />
      <el-table-column label="总价" align="center" prop="bookMoney" />
      <el-table-column label="支付方式" align="center" prop="payType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_way" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="课程状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.courses_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="核销节数" align="center" prop="usedNum" />
      <el-table-column label="过期节数" align="center" prop="expireNum" />
      <el-table-column label="退款节数" align="center" prop="backNum" />
      <el-table-column label="预约节数" align="center" prop="dateNum" />
      <el-table-column label="剩余节数" align="center" prop="leftNum" />
      <el-table-column label="付款时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约时间" align="center" prop="dateInfo"  min-width="180"/>
      <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="220">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCancel(scope.row)"
            v-hasPermi="['course:userCourses:edit']"
          >取消</el-button>
          <el-button
               size="mini"
               type="text"
               icon="el-icon-edit"
               @click="handleBackMoney(scope.row)"
               v-hasPermi="['course:userCourses:edit']"
             >申请退款</el-button>
             <el-button
               size="mini"
               type="text"
               icon="el-icon-info"
               @click="goToPage(scope.row.orderNo)"
               v-hasPermi="['course:userCourses:edit']"
             >预约详情</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学员报名对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="课程id" prop="courseId">
          <el-input v-model="form.courseId" placeholder="请输入课程id" />
        </el-form-item>
        <el-form-item label="用户成员id" prop="userMemberId">
          <el-input v-model="form.userMemberId" placeholder="请输入用户成员id" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="报名节数" prop="bookNum">
          <el-input v-model="form.bookNum" placeholder="请输入报名节数" />
        </el-form-item>
        <el-form-item label="总价" prop="bookMoney">
          <el-input v-model="form.bookMoney" placeholder="请输入总价" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.courses_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="核销节数" prop="usedNum">
          <el-input v-model="form.usedNum" placeholder="请输入核销节数" />
        </el-form-item>
        <el-form-item label="过期节数" prop="expireNum">
          <el-input v-model="form.expireNum" placeholder="请输入过期节数" />
        </el-form-item>
        <el-form-item label="退款节数" prop="backNum">
          <el-input v-model="form.backNum" placeholder="请输入退款节数" />
        </el-form-item>
        <el-form-item label="预约节数" prop="dateNum">
          <el-input v-model="form.dateNum" placeholder="请输入预约节数" />
        </el-form-item>
        <el-form-item label="剩余节数" prop="leftNum">
          <el-input v-model="form.leftNum" placeholder="请输入剩余节数" />
        </el-form-item>
        <el-form-item label="付款时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择付款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserCourses, getUserCourses, delUserCourses, addUserCourses, updateUserCourses } from "@/api/course/userCourses";

export default {
  name: "UserCourses",
  dicts: ['courses_status','pay_way'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学员报名表格数据
      userCoursesList: [],
      uploadTime: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        userId: null,
        courseId: null,
        userMemberId: null,
        price: null,
        bookNum: null,
        bookMoney: null,
        payType: null,
        status: null,
        usedNum: null,
        expireNum: null,
        backNum: null,
        dateNum: null,
        leftNum: null,
        payTime: null,
        dateInfo: null,
        date: null,
        beginDate:null,
        endDate:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        courseId: [
          { required: true, message: "课程id不能为空", trigger: "blur" }
        ],
        userMemberId: [
          { required: true, message: "用户成员id不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
        bookNum: [
          { required: true, message: "报名节数不能为空", trigger: "blur" }
        ],
        bookMoney: [
          { required: true, message: "总价不能为空", trigger: "blur" }
        ],
        payType: [
          { required: true, message: "支付方式：1微信，2积分不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        usedNum: [
          { required: true, message: "核销节数不能为空", trigger: "blur" }
        ],
        expireNum: [
          { required: true, message: "过期节数不能为空", trigger: "blur" }
        ],
        backNum: [
          { required: true, message: "退款节数不能为空", trigger: "blur" }
        ],
        dateNum: [
          { required: true, message: "预约节数不能为空", trigger: "blur" }
        ],
        leftNum: [
          { required: true, message: "剩余节数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    if (null != this.$route.query.orderNo) {
        this.queryParams.orderNo = this.$route.query.orderNo;
    }else{
        this.uploadTime[0] = this.getNearDay(-30);
        this.uploadTime[1] = this.getNearDay(0);
    }

    this.getList();
  },
  methods: {
    /** 查询学员报名列表 */
    getList() {
      this.loading = true;
       if (this.uploadTime) {
            this.queryParams.beginDate = this.uploadTime[0];
            this.queryParams.endDate = this.uploadTime[1];
        } else {
            this.queryParams.beginDate = '';
            this.queryParams.endDate = '';
        }

      listUserCourses(this.queryParams).then(response => {
        this.userCoursesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        userId: null,
        courseId: null,
        userMemberId: null,
        price: null,
        bookNum: null,
        bookMoney: null,
        payType: null,
        status: null,
        usedNum: null,
        expireNum: null,
        backNum: null,
        dateNum: null,
        leftNum: null,
        payTime: null,
        dateInfo: null,
        date: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    goToPage(orderNo) {
       this.$router.push({
           path: '/kc/userCourseInfos',
           query: { orderNo: orderNo },
        }); // 只能用 name
     },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学员报名";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserCourses(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学员报名";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUserCourses(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserCourses(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学员报名编号为"' + ids + '"的数据项？').then(function() {
        return delUserCourses(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/userCourses/export', {
        ...this.queryParams
      }, `userCourses_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
